﻿using System.Linq.Expressions;
using AvaloniaInfiniteScrolling;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using dpa.Library.Models;
using dpa.Library.Services;

namespace dpa.Library.ViewModels;

public partial class ResultViewModel : ViewModelBase
{
    private readonly IContentNavigationService _contentNavigationService;

    private Expression<Func<Poetry, bool>> _where;

    public ResultViewModel(IPoetryStorage poetryStorage,
        IContentNavigationService contentNavigationService)
    {
        _contentNavigationService = contentNavigationService;
        PoetryCollection = new AvaloniaInfiniteScrollCollection<Poetry>
        {
            OnCanLoadMore = () => _canLoadMore,
            OnLoadMore = async () =>
            {
                Status = Loading;
                var poetries = await poetryStorage.GetPoetriesAsync(_where,
                    PoetryCollection!.Count, PageSize);
                Status = string.Empty;

                if (poetries.Count() < PageSize)
                {
                    _canLoadMore = false;
                    Status = NoMoreResult;
                }

                if (PoetryCollection.Count == 0 && !poetries.Any())
                {
                    Status = NoResult;
                }

                return poetries;
            }
        };
    }

    private bool _canLoadMore;

    public override void SetParameter(object parameter)
    {
        if (parameter is not Expression<Func<Poetry, bool>> where)
        {
            return;
        }

        _where = where;
        _canLoadMore = true;
        PoetryCollection.Clear();
    }

    [ObservableProperty] private string _status;

    public const string Loading = "正在载入";

    public const string NoResult = "没有满足条件的结果";

    public const string NoMoreResult = "没有更多结果";

    public const int PageSize = 20;

    public AvaloniaInfiniteScrollCollection<Poetry> PoetryCollection { get; }

    [RelayCommand]
    private void ShowPoetry(Poetry poetry) =>
        _contentNavigationService.NavigateTo(ContentNavigationConstant.DetailView, poetry);
}